/**
 * 
 */
package com.googlecode.xcache4j.coder;

import com.googlecode.xcache4j.hessian.Hessian2Input;
import com.googlecode.xcache4j.hessian.Hessian2Output;
import com.googlecode.xcache4j.io.UnsafeByteArrayInputStream;
import com.googlecode.xcache4j.io.UnsafeByteArrayOutputStream;

/**
 * @author xy.du
 * 2015年1月11日
 */
public class HessianCoder implements Coder{

	public byte[] encode(Object obj)throws Exception {
		if(obj==null) throw new IllegalArgumentException("encode object cannot be null.");  
		
		UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(512); 
	    Hessian2Output ho = new Hessian2Output(os);  
	    ho.writeObject(obj);  
	    ho.close();
	    return os.toByteArray();
	}

	public Object decode(byte[] in)throws Exception {
		if(in==null) throw new IllegalArgumentException("decode input byte[] is null.");  
		
		UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(in);
	    Hessian2Input hi = new Hessian2Input(is);
	    return hi.readObject();  
	}  

}
